package nettyguide;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 
 * @author Shensg
 *
 * 2019年6月18日
 */
public class NettyServer {

	public static void main(String[] args) throws InterruptedException {
		// 定义一对线程组
		// 主线程组,用于接受客户端连接
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		// 从线程组,老板线程组会把任务给到从,让手下线程组去做任务
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		
		try {
			// 启动类
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			// netty 服务器创建
			serverBootstrap.group(bossGroup,workerGroup)			//主从线程组
							.channel(NioServerSocketChannel.class)	//设置nio的双向
							.childHandler(new HelloServerInitializer());					//子处理器 用于处理workerGroup
			//启动server 设置端口 ，启动方式为同步
			ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
			//监听关闭的channel 设置为同步方式
			channelFuture.channel().closeFuture().sync();
		} finally {
			//优雅关闭
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
		
		
	}

}
